package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.TaskReward;
import com.novax.ex.activity.open.api.inner.TaskRewardApi;
import com.novax.ex.activity.open.model.request.task.TaskRewardRequest;
import com.novax.ex.activity.open.model.response.task.TaskRewardInnerResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.TaskRewardService;
import com.novax.ex.common.constant.Currency;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.enums.TaskRewardType;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageQuery;
import com.novax.ex.common.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Objects;

/**
 * @Description 后台-奖励配置 controller
 * @Author ChenXi
 * @Date 2023/06/21/10:34
 */

@RestController
public class TaskRewardController extends BaseActivityController implements TaskRewardApi {

    @Resource
    private TaskRewardService taskRewardService;

    @Override
    public ReturnResult<PageUtil<TaskRewardInnerResponse>> query(PageQuery pageQuery) {
        PageUtil<TaskRewardInnerResponse> pageUtil = new PageUtil<>(pageQuery);
        taskRewardService.findPage(pageUtil, TaskRewardInnerResponse.class);
        return ReturnResult.success(pageUtil);
    }

    @Override
    public ReturnResult<?> modify(String language, Long id, TaskRewardRequest taskRewardRequest, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        if(Objects.nonNull(taskRewardRequest)){
            String currency = taskRewardRequest.getCurrency();
            Integer type = taskRewardRequest.getType();
            // 设置币种奖励, 不传递币种
            if((type.equals(TaskRewardType.currency.getType()) || type.equals(TaskRewardType.swap_fee.getType()))  && !StringUtils.isNotBlank(currency)){
                return ReturnResult.fail(getInternational(language, InternationalCode.SET_CURRENCY_WHEN_AWARD_CURRENCY));
            }
            // 设置了币种, 但是类型不为币种奖励
            if(!type.equals(TaskRewardType.currency.getType()) && !type.equals(TaskRewardType.swap_fee.getType())  && StringUtils.isNotBlank(currency)){
                return ReturnResult.fail(getInternational(language, InternationalCode.SET_CURRENCY_WHEN_AWARD_CURRENCY));
            }
            // 币种小写
            if(type.equals(TaskRewardType.currency.getType()) && StringUtils.isNotBlank(currency)){
                taskRewardRequest.setCurrency(currencyToLowerCase(taskRewardRequest.getCurrency()));
            }
            // 如果type = 1 冻结ET, 设置奖励币种为ET
            if(type.equals(TaskRewardType.frozen_et.getType())){
                taskRewardRequest.setCurrency(Currency.ET);
            }
            // 如果type = 4 冻结SCS, 设置奖励币种为SCS
            if(type.equals(TaskRewardType.frozen_scs.getType())){
                taskRewardRequest.setCurrency(Currency.SCS);
            }
            // 盲盒奖励
            if(type.equals(TaskRewardType.blindBox.getType())){
                taskRewardRequest.setCurrency(" ");
            }
            // 合约手续费抵扣券
            if(type.equals(TaskRewardType.swap_fee.getType())){
                taskRewardRequest.setCurrency(currencyToLowerCase(taskRewardRequest.getCurrency()));
            }
            // vip升级券
            if(type.equals(TaskRewardType.vip_reward.getType())){
                taskRewardRequest.setCurrency(" ");
            }
        }

        TaskReward taskReward = CopyUtils.copyObject(taskRewardRequest, TaskReward.class);
        taskReward.setId(id);
        boolean flag = taskRewardService.modifySelective(taskReward);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

}
